package com.team1.manager.school.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.team1.core.action.BaseAction;
import com.team1.core.constant.SchoolVO;
import com.team1.manager.school.entity.School;
import com.team1.manager.school.service.SchoolService;

public class SchoolAction extends BaseAction {
	private School school;
	private InputStream inputStream;
	@Resource
	private SchoolService schoolService;
	private List<School> schools;
	public String schoolUI(){
		return "schoolUI";
	}
	public String add(){
		schoolService.save(school);
		inputStream = new ByteArrayInputStream(gson.toJson(1)
				.getBytes());
		return "add";
	}
	public String show() throws UnsupportedEncodingException{
		schools = schoolService.findObjects();
		List<SchoolVO> schoolVOs = new ArrayList<SchoolVO>();
		for (School school : schools) {
			SchoolVO schoolVO = new SchoolVO(school.getId(),school.getName());
			schoolVOs.add(schoolVO);
		}
		inputStream = new ByteArrayInputStream(gson.toJson(schoolVOs).getBytes("UTF-8"));
		return "show";
	}
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public List<School> getSchools() {
		return schools;
	}
	public void setSchools(List<School> schools) {
		this.schools = schools;
	}
	
}
